//
//  BGQianMing.m
//  bigMall
//
//  Created by LTOVE on 16/7/13.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGQianMing.h"
#import "NSString+Hash.h"
#import "BGParam.h"
@implementation BGQianMing

//
//+ (void)makeQianmingWithBgParam:(BGParam *)parame
//{
//    BGQianMing *qianMing = [[BGQianMing alloc] init];
//    //设置textData
//   parame.textData =[qianMing dicToJsonStringWithDic:parame.textDataDic];
//    parame.textDataDic = nil;
//    //设置appKey
//    parame.appkey = [qianMing signPublic:parame.sendTime ver:parame.ver encryption:parame.encryption module:parame.module dir:parame.dir controller:parame.controller action:parame.action];
//    
//    
//}
/**
 *  字典转json
 *
 *  @param dict 要转换的字典
 *
 *  @return 转换后的json字符串
 */
- (NSString *)dicToJsonStringWithDic:(NSDictionary *)dict
{
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization
                        dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
    NSString *jsonString;
    if ([jsonData length] > 0 && error == nil){
        GWLog(@"Successfully serialized the dictionary into data.");
        //NSData转换为String
        jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
        jsonString = [jsonString stringByReplacingOccurrencesOfString:@" " withString:@""];
        //            GWLog(@"JSON String = %@", jsonString);
    }
    else if ([jsonData length] == 0 &&
             error == nil){
        //        GWLog(@"No data was returned after serialization.");
        jsonString = @" ";
    }
    else if (error != nil){
        //        GWLog(@"An error happened = %@", error);
        jsonString = @" ";
    }
    return jsonString;
}
/**
 *  签名方法
 *
 *  @return 加密后的MD5字符串
 */
- (NSString *)signPublic:(NSString *)sendTime ver:(NSString *)ver encryption:(NSString *)encryption module:(NSString *)module dir:(NSString *)dir controller:(NSString *)controller action:(NSString *)action
{
    NSString *keyA = [sendTime substringWithRange:NSMakeRange(0, 5)];
    NSString *keyB = [sendTime substringWithRange:NSMakeRange(5, 5)];
    NSString *str = [NSString stringWithFormat:@"%@%@%@%@%@%@%@%@",ver,keyA,encryption,module,dir,keyB,controller,action];
    
    
    return str.md5String;
}
@end
